//
//  WBWelcomeController.swift
//  sinaWeibo
//
//  Created by 吴熠 on 16/11/16.
//  Copyright © 2016年 吴熠. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage


private let bottomMargin: CGFloat = 120

class WBWelcomeController: UIViewController {
        
        override func viewDidLoad() {
            super.viewDidLoad()
            //设置UI界面
            //根据自动布局设置的约束在什么才会生效呢? --> viewDidLayoutSubviews才会根据自动布局
            //自动布局的控件有frame? 一定有,能够直接修改frame,需要修改约束,间接的修改frame
            setupUI()
            
            //线不要在这个地方
            //startAnimtion()
            
        }
        
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            //自动布局的动画效果应该在 viewDidAppera中执行才会有正确的动画效果
            startAnimtion()
        }
        
        private func startAnimtion() {
            let offsetY = -(UIScreen.main.bounds.height - bottomMargin - iconView.bounds.height)
            //执行动画效果
            /*
             弹簧动画效果
             1.usingSpringWithDamping: 阻尼系数 0 ~ 1之间 越小越弹
             2.initialSpringVelocity 加速度 值越大加速度就越大 9.8
             3.options 动画的可选项 OC中传0 swift中传空数组 []
             */
            //更新约束只是提前收集了约束的变化, 在viewDidLayoutSubviews约束才会生效
            //layoutIfNeeded() 提前刷新页面 viewDidLayoutSubviews
            self.iconView.snp.updateConstraints({ (make) in
                make.bottom.equalTo(self.view).offset(offsetY)
            })
            UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 7, options: [], animations: {
                //执行动画效果
                //更新约束
                //提前刷新页面
                self.view.layoutIfNeeded()
                
            }, completion: { (finished) -> () in
                //动画效果完成的回调
                UIView.animate(withDuration: 0.5, animations: {
                    self.welcomeLabel.alpha = 1
                }, completion: { (_) -> () in
//                    print("👌")
//                    UIApplication.shared.keyWindow?.rootViewController = WBTarBarController()
                    //欢迎界面动画完成后发出通知告诉接下来显示的根视图控制器
                    NotificationCenter.default.post(name: NSNotification.Name(kNotification), object: nil)
                })
            })
            
        }
        
        private func setupUI() {
            
            self.view.backgroundColor = UIColor.white
            
            self.view.addSubview(iconView)
            self.view.addSubview(welcomeLabel)
            //设置约束
            iconView.snp.makeConstraints { (make) in
                make.centerX.equalTo(self.view)
                make.bottom.equalTo(self.view).offset(-bottomMargin)
                make.size.equalTo(CGSize(width: 85, height: 85))
            }
            
            welcomeLabel.snp.makeConstraints { (make) in
                make.centerX.equalTo(iconView)
                make.top.equalTo(iconView.snp.bottom).offset(20)
            }
            
            //隐藏文字
            welcomeLabel.alpha = 0
            
            //设置iconView为圆角
            //85
            iconView.layer.cornerRadius = 85 * 0.5
            iconView.layer.masksToBounds = iconView.layer.cornerRadius > 0
            
            iconView.sd_setImage(with: WBUserAccountViewModel.shared.hearurl!)
//            iconView.setImageWith(WBUserAccountViewModel.shared.hearurl!)
        }
        
        private lazy var iconView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big"))
        private lazy var welcomeLabel: UILabel = UILabel(title: "欢迎归来", fontSize: 16, textColor: UIColor.darkGray)
}
